package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ZipUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.*;
import cn.stylefeng.guns.sys.modular.system.factory.LayuiTreeFactory;
import cn.stylefeng.guns.sys.modular.system.mapper.GcgWsjdTakeawayMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.GcgWsjdYinhangkaMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.GcgWsjdParam;
import cn.stylefeng.guns.sys.modular.system.model.params.GcgWsjdShenpiParam;
import cn.stylefeng.guns.sys.modular.system.model.params.GcgWsjdYinhangkaParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ProjectParam;
import cn.stylefeng.guns.sys.modular.system.model.result.GcgWsjdResult;
import cn.stylefeng.guns.sys.modular.system.model.result.GcgWsjdYinhangkaResult;
import cn.stylefeng.guns.sys.modular.system.service.GcgWsjdService;
import cn.stylefeng.guns.sys.modular.system.service.GcgWsjdYinhangkaService;
import cn.stylefeng.guns.sys.modular.system.service.PushMsgService;
import cn.stylefeng.guns.sys.modular.system.warpper.MenuWrapper;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.guns.sys.modular.system.service.GcgWsjdShenpiService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.treebuild.DefaultTreeBuildFactory;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 网上建店信息控制器
 * @author liuy
 * @Date 2021-05-18 13:30:40
 */
@Controller
@RequestMapping("/gcgWsjd")
public class GcgWsjdController extends BaseController {

    private String PREFIX = "/modular/system/gcgWsjd";

    @Autowired
    private GcgWsjdService gcgWsjdService;
    @Resource
    private GcgWsjdShenpiService shenpiService;
    @Resource
    private GcgWsjdYinhangkaMapper yinhangkaMapper;
    @Resource
    private PushMsgService pushMsgService;
    @Resource
    private GcgWsjdTakeawayMapper gcgWsjdTakeawayMapper;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/gcgWsjd.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/gcgWsjd_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam Long gcgWsjdId, Model model) {
        model.addAttribute("gcgWsjdId",gcgWsjdId);
        return PREFIX + "/gcgWsjd_edit.html";
    }

    @RequestMapping("/email")
    public String email(@RequestParam Long gcgWsjdId, Model model) {
        model.addAttribute("gcgWsjdId",gcgWsjdId);

        GcgWsjd detail = this.gcgWsjdService.getById(gcgWsjdId);
        if(detail.getYjzt()==1){
            detail.setName("成功");
        }else{
            detail.setName("失败");
        }
        model.addAllAttributes(BeanUtil.beanToMap(detail));
        LogObjectHolder.me().set(detail);
        return PREFIX + "/gcgWsjd_email.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(GcgWsjdParam gcgWsjdParam) {
        this.gcgWsjdService.add(gcgWsjdParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(GcgWsjdParam gcgWsjdParam) {
        this.gcgWsjdService.update(gcgWsjdParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(GcgWsjdParam gcgWsjdParam) {
        this.gcgWsjdService.delete(gcgWsjdParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(GcgWsjdParam gcgWsjdParam) {
        GcgWsjd detail = this.gcgWsjdService.getById(gcgWsjdParam.getGcgWsjdId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2021-05-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(GcgWsjdParam gcgWsjdParam) {
        return this.gcgWsjdService.findPageBySpec(gcgWsjdParam);
    }

    @RequestMapping(value = "/confirmOk")
    @ResponseBody
    public Object confirmOk(@RequestParam Long id) {
        //通过id查询审批表中type=1的数据
        GcgWsjdShenpi shenpi=shenpiService.getOne(new QueryWrapper<GcgWsjdShenpi>().eq("gcg_wsjd_id",id).eq("type",1));
        if(ToolUtil.isNotEmpty(shenpi)){
          shenpi.setShStatus("3");
          shenpi.setShenpiRen(LoginContextHolder.getContext().getUser().getName());
          shenpi.setShenpiYijina("审核通过");
          shenpi.setShenpiShijian(new Date());
          boolean b = shenpiService.updateById(shenpi);

          if (b){
              //推送公众号消息
              String now = DateUtil.getNow();//当前时间
              //获取当前店名
              Map map = gcgWsjdTakeawayMapper.getDm(id);
              String dm = null;
              String phone = null;
              if (ToolUtil.isNotEmpty(map)){
                  if (ToolUtil.isNotEmpty(map.get("mingcheng"))){
                      dm = map.get("mingcheng").toString();
                  }
                  if (ToolUtil.isNotEmpty(map.get("phone"))){
                      phone = map.get("phone").toString();
                  }
              }

              if (ToolUtil.isNotEmpty(phone)){
                  //查询当前店审核状态(type: 1 美团团购2 支付宝 3 外卖)
                  String shStatus = gcgWsjdTakeawayMapper.getStatus(Long.valueOf(map.get("gcgWsjdId").toString()), 1);

                  GcgWsjd gcgWsjd = new GcgWsjd();
                  gcgWsjd.setMingcheng(dm);
                  gcgWsjd.setGcgProjectId(Integer.valueOf(map.get("gcgProjectId").toString()));
                  gcgWsjd.setGcgWsjdId(Long.valueOf(map.get("gcgWsjdId").toString()));
                  gcgWsjd.setYaoma(map.get("yaoma").toString());
                  gcgWsjd.setShStatus(shStatus);

                  //推送小妖app信息,即时通讯
                  pushMsgService.mttgToIM(gcgWsjd, phone, "通过", now);
              }
          }
        }

        return SUCCESS_TIP;
    }

    /**
     * @Title: expExcel
     * @Description: 批量导出美团团购信息
     * @param @param params
     * @param @param response
     * @return void
     */
    @RequestMapping("/expExcel")
    @ResponseBody
    public void expExcel(HttpServletResponse response, GcgWsjdParam wsjdParam) throws Exception {
        gcgWsjdService.expExcel(response,wsjdParam);
//     Integer flag=  gcgWsjdService.expExcel(response,wsjdParam);
//     return flag;
    }

    @RequestMapping(value = "/layuiTree")
    @ResponseBody
    public List<LayuiTreeNode> layuiTree() {

        List<LayuiTreeNode> list = this.gcgWsjdService.layuiTree();
        list.add(LayuiTreeFactory.createRoot());

        DefaultTreeBuildFactory<LayuiTreeNode> treeBuildFactory = new DefaultTreeBuildFactory<>();
        treeBuildFactory.setRootParentId("-1");
        return treeBuildFactory.doTreeBuild(list);
    }

    @RequestMapping(value = "/listTree")
    @ResponseBody
    public Object listTree(@RequestParam(required = false) Long gcgWsjdId) {
        GcgWsjdParam wsjdParam=new GcgWsjdParam();
        wsjdParam.setGcgWsjdId(gcgWsjdId);
        List<Map<String, Object>> menus = this.gcgWsjdService.selectMenuTree(wsjdParam);
        List<Map<String, Object>> menusWrap = new MenuWrapper(menus).wrap();

        LayuiPageInfo result = new LayuiPageInfo();
        result.setData(menusWrap);
        return result;
    }

    @RequestMapping(value = "/selectMenuTreeList")
    public List<ZTreeNode> selectMenuTreeList() {
        List<ZTreeNode> roleTreeList = this.gcgWsjdService.menuTreeList();
        roleTreeList.add(ZTreeNode.createParent());
        return roleTreeList;
    }



    /**
     * 支付宝列表
     *
     */
    @RequestMapping(value = "/zfbList")
    @ResponseBody
    public Object zfbList(GcgWsjd gcgWsjd) {
        Page<Map<String, Object>> list = this.gcgWsjdService.getZfb(gcgWsjd);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 支付宝审批通过
     *
     */
    @RequestMapping("/zfbPass")
    @ResponseBody
    public ResponseData zfbPass(@RequestParam("ids") String ids) {
        String[] idss = ids.split(",");
        for (String id : idss) {
            this.gcgWsjdService.zfbPass(id);
        }
        return ResponseData.success();
    }

    /**
     * 支付宝审批驳回
     *
     */
    @RequestMapping("/zfbReject")
    @ResponseBody
    public ResponseData zfbReject(@RequestParam("id") Long gcgWsjdId, @RequestParam("data") String data) {
        this.gcgWsjdService.zfbReject(gcgWsjdId, data);
        return ResponseData.success();
    }

    @RequestMapping("/rejectMt")
    @ResponseBody
    public ResponseData rejectMt(@RequestParam("id") Long gcgWsjdId, @RequestParam("data") String data) {
        this.gcgWsjdService.reject(gcgWsjdId, data);
        return ResponseData.success();
    }

    /**
     * 支付宝审批驳回列表
     *
     */
    @RequestMapping("/rejectList")
    @ResponseBody
    public Object rejectList(Long gcgWsjdId) {
        List<Map<String, Object>> menus = this.gcgWsjdService.rejectList(gcgWsjdId);
        List<Map<String, Object>> menusWrap = new MenuWrapper(menus).wrap();

        LayuiPageInfo result = new LayuiPageInfo();
        result.setData(menusWrap);
        return result;
    }


    @RequestMapping(value = "/rejectTreeList")
    public List<ZTreeNode> rejectTreeList() {
        List<ZTreeNode> roleTreeList = this.gcgWsjdService.rejectTreeList();
        roleTreeList.add(ZTreeNode.createParent());
        return roleTreeList;
    }

    /**
     * 支付宝审批驳回页面
     * @return
     */
    @RequestMapping("/reject")
    public String reject(@RequestParam Long gcgWsjdId, Model model) {
        model.addAttribute("gcgWsjdId",gcgWsjdId);
        return "/modular/system/projectwsjd/reject.html";
    }

    @RequestMapping("/yhkList")
    @ResponseBody
    public List<GcgWsjdYinhangkaResult> yhkList() {
        GcgWsjdYinhangkaParam param=new GcgWsjdYinhangkaParam();
        List<GcgWsjdYinhangkaResult>list=yinhangkaMapper.customList(param);
        return list;
    }

    /**
     * 指定营运经理页面
     */
    @RequestMapping("/zdkhr")
    public String zdkhr(String gcgWsjdYinhangkaId, Model model) {
        //model.addAttribute("gcgWsjdId", gcgWsjdId);
        model.addAttribute("gcgWsjdYinhangkaId", gcgWsjdYinhangkaId);
        return PREFIX + "/selectKhr.html";
    }

    @RequestMapping("/saveKhr")
    @ResponseBody
    public ResponseData saveKhr(GcgWsjdParam param) {

        gcgWsjdService.saveKhr(param);
        return ResponseData.success();
    }


    /**
     * 批量下载图片
     */
    @RequestMapping(value = "/btnDown")
    @ResponseBody
    public Object btnDown(@RequestParam("ids") String ids, HttpServletRequest request, HttpServletResponse response) throws Exception {
        this.gcgWsjdService.btnDown(ids,request,response);
        String urls="D:/wsjd/zfb/zfbtp/";
        ZipUtil.zip(urls);
        return SUCCESS_TIP;
    }

}


